import unittest
import wsgiref.util
from purest.web.request import Request

class RequestTest(unittest.TestCase):
    
    def setUp(self):
        environ = {}
        wsgiref.util.setup_testing_defaults(environ)
        environ["QUERY_STRING"] = "a=1&a=2&b=3"
        environ["HTTP_COOKIE"] = "emos=130307052; jcvid=AA:true:1; mid=BAA32AA8"
        self.request_to_test = Request(environ)
    
    def test_extract_last_query_param_of_multiple_with_same_name(self):
        self.assertEqual(self.request_to_test.query_params["a"], "2")
        
    def test_get_all_query_params_with_the_same_name(self):
        self.assertEqual(self.request_to_test.query_params.get_all("a"), 
                         ["2", "1"])
        
    def test_get_cookies(self):
        self.assertEqual(self.request_to_test.cookies["emos"], "130307052")
        self.assertEqual(self.request_to_test.cookies["jcvid"], "AA:true:1")
        self.assertEqual(self.request_to_test.cookies["mid"], "BAA32AA8")
        
    def test_extract_http_headers(self):
        expected = {"COOKIE": "emos=130307052; jcvid=AA:true:1; mid=BAA32AA8",
                    "HOST": "127.0.0.1"}
        self.assertDictEqual(self.request_to_test.headers, expected)